/*
 * @Author: mikey.zhaopeng
 * @Date:   2016-02-03 14:57:46
 * @Last Modified by:   mikey.zhaopeng
 * @Last Modified time: 2016-04-05 19:24:33
 */

define([
    'static',
    './management/setting',
    './management/monitor'
], function(
    CONST,
    setting,
    monitor
) {
    extend(Body, Page); //继承 保持和 class-instance 分离语言的继承感觉。 放在最上面，因为
    function Body() {
        var _this = this;
        var _super = arguments.callee.parent;
        /**
         ** 初始化页面配置
         **/
        this.init = function() {
            this._DOM = $("[data-piece='content']"); //当前这个类 hold住的前端引用
            this.children = [setting, monitor]; //获得 piece 实例的引用
            _super.init.call(_this);


            var vscroll = this._DOM.find(".vertical-scroll");
            var sh = $('.fmp-setting').height();
            var th = this._DOM.find(".treable-head").height(); //列表头高50
            var hb = this._DOM.find(".treable-body").height(); //列表内容部分高
            var h = this._DOM.height(); //article的高度
            vscroll.on("dragstart", function() {
                return false;
            }); //消除 默认h5 拖拽产生的影响
            vscroll.toggleClass("show", (sh + th + hb) > h ? true : false);
            if ((sh + th + hb) > h) { //显示纵向滚动条
                vscroll.children(":first").css("height", (sh + th + hb) / h * 100 + "px");
            }

            this._DOM.on("mousewheel", function(e) {
                e.stopImmediatePropagation();
                var thumb = vscroll.find(".vertical-thumb");
                var tdim = thumb.get(0).getBoundingClientRect();
                var sdim = vscroll.get(0).getBoundingClientRect();
                if (e.originalEvent.wheelDelta > 0) { //向下
                    thumb.css("top", (tdim.top - sdim.top + 5) + "px");
                    var h = tdim.top - sdim.top;
                    if (h < 0) h = 0;
                    e.data = { h: h };
                } else { //向上
                    thumb.css("top", (tdim.top - sdim.top - 5) + "px");
                    h = tdim.top - sdim.top;
                    if (h < 0) h = 0;
                    e.data = { h: h };
                }
                /***
                 ** 超出边界的控制
                 ***/
                if (tdim.top < sdim.top) {
                    thumb.css("top", 0);
                } else if (tdim.bottom > sdim.bottom) {
                    thumb.css("top", (sdim.bottom - tdim.height - sdim.top) + "px");
                }
                _this.children.forEach(function(item) {
                    item.update(e);
                });

            });

            /***
             ** 进度条thumb mousedown 点击
             **/
            vscroll.children(":first").unbind("mousedown").mousedown(function(e) {
                e.stopImmediatePropagation();
                console.log('2342344');
                var thumb = $(this);
                var sdim = vscroll.get(0).getBoundingClientRect();
                var start = e.clientY;
                /***
                 ** 鼠标拖动
                 **/
                vscroll.unbind("mousemove").mousemove(function(e) {
                    e.stopImmediatePropagation();
                    console.log('2342344');
                    var end = e.clientY;
                    var tdim = thumb.get(0).getBoundingClientRect();
                    var m = end - start;
                    start = end;
                    thumb.css("top", (tdim.top - sdim.top + m) + "px");
                    /***
                     ** 超出边界的控制
                     ***/
                    if (tdim.top < sdim.top) {
                        thumb.css("top", 0);
                    } else if (tdim.bottom > sdim.bottom) {
                        thumb.css("top", (sdim.bottom - tdim.height - sdim.top) + "px");
                    }

                    var h = tdim.top - sdim.top;
                    if (h < 0) h = 0;
                    e.data = { h: h };
                    _this.children.forEach(function(item) {
                        item.update(e);
                    });
                });
            });

            /***
             ** 点击滚动条空白处
             ***/
            vscroll.click(function(e) {
                e.stopImmediatePropagation();
                console.log('2342344');
                var thumb = vscroll.find(".vertical-thumb");
                var tdim = thumb.get(0).getBoundingClientRect();
                var sdim = vscroll.get(0).getBoundingClientRect();

                if (e.clientY >= tdim.top) {
                    thumb.css("top", (tdim.top - sdim.top + 20) + "px");
                } else {
                    thumb.css("top", (tdim.top - sdim.top - 20) + "px");
                }
                /***
                 ** 超出边界的控制
                 ***/
                if (tdim.top < sdim.top) {
                    thumb.css("top", 0);
                } else if (tdim.bottom > sdim.bottom) {
                    thumb.css("top", (sdim.bottom - tdim.height - sdim.top) + "px");
                }

                var h = tdim.top - sdim.top;
                if (h < 0) h = 0;
                e.data = { h: h };
                _this.children.forEach(function(item) {
                    item.update(e);
                });
            });

            vscroll.mouseleave(function(e) {
                vscroll.unbind("mousemove");
                console.log('2342344');
                var lf = vscroll.find(".vertical-thumb").css("top");
                vscroll.find(".vertical-thumb").css("top", lf + "px");
            });

            vscroll.mouseup(function(e) {
                vscroll.unbind("mousemove");
                console.log('2342344');
                var thumb = $(this).children(":first");
                var dim1 = $(this).get(0).getBoundingClientRect();
                var dim2 = thumb.get(0).getBoundingClientRect();
                if (dim2.bottom >= dim1.bottom) {
                    var l = (dim1.bottom - dim2.height) - dim1.top;
                    thumb.css("top", l + "px");
                } else if (dim2.top <= dim1.top) {
                    thumb.css("top", 0);
                }
            });
            this.event();
        };

        this.event = function() {
            this._DOM.off('setting-click').on('setting-click', function(e, params) {
                monitor.updateTable(params);
            });
        }

        /**
         ** 处理用户交互事件，引起的dom更新
         **/
        this.update = function(msg) {
            console.log(msg);
            _super.update.call(this, msg);
            switch (msg.type) {
                case "SHRINK_EXPAND":
                    this._DOM.toggleClass("shrink-expand", msg.data.status);
                    break;
            }
        }
    }
    return new Body();
});
